package com.lzw.bluetoothpractice.Activitys;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lzw.bluetoothpractice.Adapters.MyForumListAdapter;
import com.lzw.bluetoothpractice.Adapters.RecoveryForumListAdapter;
import com.lzw.bluetoothpractice.AppConfig;
import com.lzw.bluetoothpractice.Models.Forum;
import com.lzw.bluetoothpractice.R;
import com.lzw.bluetoothpractice.Utils.AppUtil;
import com.lzw.bluetoothpractice.Utils.SharePreferenceUtils;
import com.lzw.bluetoothpractice.bean.ForumListBean;
import com.lzw.bluetoothpractice.network.HeadAssmblyUtils;
import com.lzw.bluetoothpractice.network.RetrofitManager;
import com.lzw.bluetoothpractice.network.api.RequestApi;

import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import static com.lzw.bluetoothpractice.MyApplication.getContext;


public class MyForumActivity extends BaseActivity {
    private Map<String, String> keyMap;
    RecyclerView rvRecoveryForumList;  //ListView
    MyForumListAdapter recoveryActionListAdapter;  //适配器
    public List<Forum> forums;  //用于存放获取的论坛信息
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_forum);
        rvRecoveryForumList=fd(R.id.rcForumList);
        InitView();
        initForum();
        //添加点击事件

    }
    private  void InitView(){
        InitNavBar2(true,"参与话题",false);




    }
    private void initForum() {
        try {
            keyMap = HeadAssmblyUtils.HeadAssembly("/dev-api/android/forum/list");
        } catch (Exception e) {
            e.printStackTrace();
        }
        String accessToken = SharePreferenceUtils.getString(getContext(), "access_token", null);
        String userid = SharePreferenceUtils.getString(getContext(),"userId",null);

        RetrofitManager.getInstance(AppConfig.BASE_URL)
                .createReq(RequestApi.class).forumListByUserId(Long.parseLong(userid) ,"1", AppUtil.getVersionCode() + "", accessToken, keyMap.get("gmtStr"), keyMap.get("contentMD5"), keyMap.get("hmacCode")).
                observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<JSONObject>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

                Log.e("论坛：", e.getMessage() + "");

            }

            @Override
            public void onNext(JSONObject json) {

                ForumListBean forumListBean = JSON.parseObject(json.toString(), ForumListBean.class);
                if (forumListBean.getCode() == 200) {

                    //Article article=new Article();
                    LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(MyForumActivity.this);
                    linearLayoutManager2.setOrientation(LinearLayoutManager.VERTICAL);
                    rvRecoveryForumList.setLayoutManager(linearLayoutManager2);
                    recoveryActionListAdapter = new MyForumListAdapter(MyForumActivity.this);
                    recoveryActionListAdapter.updateData(forumListBean.getRows());
                    rvRecoveryForumList.setAdapter(recoveryActionListAdapter);


                } else {
                    Toast toast = Toast.makeText(MyForumActivity.this, "查询论坛信息失败", Toast.LENGTH_LONG);
                    toast.show();
                }


            }
        });

    }
}
